using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MallRechargePanel : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_MallRechargePanel
        {
            #region ObjectBinding Generate 
            public UnityEngine.UI.ExLoopHorizontalScrollRect mallRechargeLoopList { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopHorizontalScrollRect>("mallRechargeLoopList", out var __tbv0);
                this.mallRechargeLoopList = __tbv0;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIN_MallRechargePanel),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields

        private CfgMallType cfgMallType = null;
        private List<int> mallItemCidList = null;
        private LoopScrollAdapter<UIN_MallRechargeCell> mallRecharges = null;
        #endregion fields

        #region properties

        protected UIB_UIN_MallRechargePanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MallRechargePanel();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.mallRecharges = this.InitLoopScroll<UIN_MallRechargeCell>(this.ui.mallRechargeLoopList, this.OnItemChanged);
            this.mallRecharges.InitUnitSelected(UnitSelectType.ByExButton, this.onItemSelected);
            this.mallItemCidList = ListPool<int>.Get();
        }

        protected override void OnShow(UIBaseData data)
        {

        }

        protected override void OnHide()
        {
            this.mallItemCidList?.Clear();
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.cfgMallType = null;
            if (this.mallItemCidList != null)
            {
                ListPool<int>.Put(this.mallItemCidList);
            }
            this.mallItemCidList = null;
            this.mallRecharges = null;
        }

        #region methonds

        public void Show(CfgMallType cfgMallType)
        {
            this.cfgMallType = cfgMallType;
            this.Show();
            this.InitDataList();
            this.ShowList();
        }

        private void InitDataList()
        {
            this.mallItemCidList.Clear();
            if (TableCenter.mallItem.MallItemMap.TryGetValue(this.cfgMallType.Id, out var mallItemCids))
            {
                for (int i = 0; i < mallItemCids.Count; i++)
                {
                    int mallItemCid = mallItemCids[i];
                    if (MallModule.Instance.CheckMallItemIsCanShow(mallItemCid))
                    {
                        this.mallItemCidList.Add(mallItemCid);
                    }
                }
            }
        }

        private void ShowList()
        {
            this.mallRecharges.RefillCells(this.mallItemCidList.Count);
        }

        private void OnItemChanged(UIN_MallRechargeCell item, int index)
        {
            item.Show(this.mallItemCidList[index]);
        }

        private void onItemSelected(int index, UIN_MallRechargeCell item)
        {
            this.OpenUI<UIBuyPanel>(UD_BuyPanel.Create(item.mallItemCid));
        }
        #endregion methonds
    }
}
